<template>
  <div>
    <div style="margin-top: 10px;display: flex;justify-content: center">
      <el-input
        v-model="keywords"
        placeholder="通过用户名搜索用户..."
        prefix-icon="el-icon-search"
        style="width: 400px;margin-right: 10px"
        @keydown.enter.native="doSearch"
      />
      <el-button
        icon="el-icon-search"
        type="primary"
        @click="doSearch"
      >
        搜索
      </el-button>
    </div>
    <div class="admin-container">
      <el-card
        v-for="(admin,index) in admins"
        :key="index"
        class="admin-card"
      >
        <div
          slot="header"
          class="clearfix"
        >
          <span>{{ admin.name }}</span>
          <el-button
            style="float: right; padding: 3px 0;color: #e30007;"
            type="text"
            icon="el-icon-delete"
            @click="deleteAdmin(admin)"
          />
        </div>
        <div>
          <div class="img-container">
            <img
              :src="admin.userface"
              :alt="admin.name"
              :title="admin.name"
              class="adminface-img"
            >
          </div>
          <div class="admininfo-container">
            <div>用户名：{{ admin.name }}</div>
            <div>手机号码：{{ admin.phone }}</div>
            <div>电话号码：{{ admin.telephone }}</div>
            <div>地址：{{ admin.address }}</div>
            <div>
              用户状态：
              <el-switch
                v-model="admin.enable"
                active-text="启用"
                active-color="#13ce66"
                inactive-color="#ff4949"
                inactive-text="禁用"
                @change="enableChange(admin)"
              />
            </div>
            <div>
              用户角色：
              <el-tag
                v-for="(role,indexj) in admin.roles"
                :key="indexj"
                type="success"
                style="margin-right: 4px"
              >
                {{ role.name_zh }}
              </el-tag>
              <el-popover
                placement="right"
                title="角色列表"
                width="200"
                trigger="click"
                @show="showPop(admin)"
                @hide="hidePop(admin)"
              >
                <el-select
                  v-model="selectedRoles"
                  multiple
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(r,indexk) in allRoles"
                    :key="indexk"
                    :label="r.name_zh"
                    :value="r.id"
                  />
                </el-select>
                <el-button
                  slot="reference"
                  icon="el-icon-more"
                  type="text"
                />
              </el-popover>
            </div>
            <div>备注：{{ admin.remark }}</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
  import {getAllRoles,searchAdmin,deleteAdmin} from '@/api/system'
  import {editRoles, putAdmin} from "../../../api/system";
  import request from '@/utils/request'
  export default {
    name: "Sysadmin",
    data() {
      return {
        keywords: '',
        admins: [],
        selectedRoles: [],
        allRoles: []
      }
    },
    mounted() {
      request({
        url:'/doLogin'
      })
      this.initAdmins();
    },
    methods: {
      deleteAdmin(admin) {
        this.$confirm('此操作将永久删除【'+admin.name+'】, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAdmin(admin.id).then(resp=>{
            if (resp) {
              this.initAdmins();
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      doSearch() {
        this.initAdmins();
      },
      hidePop(admin) {
        let roles = [];
        Object.assign(roles, admin.roles);
        let flag = false;
        if (roles.length != this.selectedRoles.length) {
          flag = true;
        } else {
          for (let i = 0; i < roles.length; i++) {
            let role = roles[i];
            for (let j = 0; j < this.selectedRoles.length; j++) {
              let sr = this.selectedRoles[j];
              if (role.id == sr) {
                roles.splice(i, 1);
                i--;
                break;
              }
            }
          }
          if (roles.length != 0) {
            flag = true;
          }
        }
        if (flag) {
          let url = '/system/admin/role/edit/'+ admin.id + '?roleIds=' +this.selectedRoles;
          /*this.selectedRoles.forEach(sr => {
            url += '&rids=' + sr;
          });*/
          editRoles(admin.id,this.selectedRoles).then(resp => {
            if (resp) {
              this.initAdmins();
            }
          });
        }
      },
      showPop(admin) {
        this.initAllRoles();
        let roles = admin.roles;
        this.selectedRoles = [];
        roles.forEach(r => {
          this.selectedRoles.push(r.id);
        })
      },
      enableChange(admin) {
        delete admin.roles;
        putAdmin(admin).then(resp => {
          if (resp) {
            this.initAdmins();
          }
        })
      },
      initAllRoles() {
        getAllRoles.then(resp => {
          if (resp) {
            this.allRoles = resp;
          }
        })
      },
      initAdmins() {
        searchAdmin(this.keywords).then(resp => {
          if (resp) {
            this.admins = resp;
          }
        })
      }
    }
  }
</script>

<style>
  .admininfo-container div {
    font-size: 12px;
    color: #409eff;
  }

  .admininfo-container {
    margin-top: 20px;
  }

  .img-container {
    width: 100%;
    display: flex;
    justify-content: center;
  }

  .adminface-img {
    width: 72px;
    height: 72px;
    border-radius: 72px;
  }

  .admin-container {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    text-align: left;
  }

  .admin-card {
    width: 350px;
    margin-bottom: 20px;
  }
</style>
